﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using WeberTimeTracker.Models;

namespace WeberTimeTracker.Controllers
{
    public class PayController : Controller
    {

        // GET: /Pay/

        public ActionResult Index()
        {
            using (TimeTrackerContext data = new TimeTrackerContext())
            {
                var items = GetTransactionData();
                return View(items);
            }
        }

        // GET: /Pay/Edit/5

        public ActionResult Edit(int id, string Project, DateTime startDate, DateTime endDate)
        {
			
            using (TimeTrackerContext data = new TimeTrackerContext())
            {
                int project_id = (from n in data.Projects where n.Project_Name == Project select n.Project_ID).Sum();
                var TransactionToEdit = data.Database.SqlQuery<PeriodHours>("[dbo].[period_hours] @p0, @p1, @p2, @p3", startDate, endDate, id, project_id).ToList();
				return View(TransactionToEdit.Single());
            }
        }

        // POST: /Pay/Edit/5

        [HttpPost]
		public ActionResult Edit(int id, string Project, PeriodHours collection, DateTime startDate, DateTime endDate)
        {
            
                using (TimeTrackerContext data = new TimeTrackerContext())
                {
                    int project_id = (from n in data.Projects where n.Project_Name == Project select n.Project_ID).Sum();


                    var TransactionToupdate = (from n in data.Times where n.User_ID == id && n.Project_ID == (project_id) && n.Date >= (startDate) && n.Date <= (endDate) select n).ToList();
                    foreach (Time d in TransactionToupdate)
                    {
                        d.Paid = true;
                    }
					
					data.SaveChanges();
					return RedirectToAction("Index");
                }
        }

        private static IEnumerable<PeriodHours> GetTransactionData()
        {
            using (TimeTrackerContext data = new TimeTrackerContext())
            {
                var result2 = data.Database.SqlQuery<PeriodHours>("[dbo].[period_hours] @p0, @p1", "1/1/0001", "1/1/9999").ToList();
                return result2;
            }
        }

        private static IEnumerable<PeriodHours> GetTransactionData2(DateTime sdate, DateTime edate)
        {
            using (TimeTrackerContext data = new TimeTrackerContext())
            {
                var result2 = data.Database.SqlQuery<PeriodHours>("[dbo].[period_hours] @p0, @p1", sdate, edate).ToList();
                return result2;
            }
        }
        public ActionResult GetList(DateTime sdate, DateTime edate)
        {
            var items = GetTransactionData2(sdate, edate);

			ViewBag.StartDate = sdate;
			ViewBag.EndDate = edate;

            return PartialView(items);
        }
    }
}
